package com.gs.synedocket.fragment;

import android.content.Intent;
import android.os.Handler;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.gs.synedocket.R;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.constant.EventCode;
import com.gs.synedocket.db.bean.Event;
import com.gs.synedocket.db.dao.LogUtil;
import com.gs.synedocket.fragment.base.BaseChildFragment;
import com.gs.synedocket.fragment.docket.DocketFragment;
import com.gs.synedocket.fragment.docket.DocketLogDetailFragment;
import com.gs.synedocket.fragment.driver.DriverFragment;
import com.gs.synedocket.util.AppUtil;
import com.gs.synedocket.util.DocketUtil;
import com.gs.synedocket.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class DocketOnHandFragment extends BaseChildFragment {

    private List<Fragment> fragmentList;
    private List<String> fragmentTag;
    private Fragment mFragment;
    private int fragmentIndex;
    private RadioButton rbDocket, rbDriver, rbQRCode;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_docket_on_hand;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        fragmentList = new ArrayList<>();
        fragmentTag = new ArrayList<>();
        DocketFragment docketFragment = new DocketFragment();
        DriverFragment driverFragment = new DriverFragment();
        QRCodeFragment qrCodeFragment = new QRCodeFragment();
        DocketLogDetailFragment docketLogDetailFragment = new DocketLogDetailFragment();
        fragmentList.add(docketFragment);
        fragmentList.add(driverFragment);
        fragmentList.add(qrCodeFragment);
        fragmentList.add(docketLogDetailFragment);
        fragmentTag.add(DocketFragment.class.getSimpleName());
        fragmentTag.add(DriverFragment.class.getSimpleName());
        fragmentTag.add(QRCodeFragment.class.getSimpleName());
        fragmentTag.add(DocketLogDetailFragment.class.getSimpleName());
        rbDocket = fd(R.id.rb_docket);
        rbDriver = fd(R.id.rb_driver);
        rbQRCode = fd(R.id.rb_code);
        MyOnRBCheckedChangeListener onRBCheckedChangeListener = new MyOnRBCheckedChangeListener();
        rbDocket.setOnCheckedChangeListener(onRBCheckedChangeListener);
        rbDriver.setOnCheckedChangeListener(onRBCheckedChangeListener);
        rbQRCode.setOnCheckedChangeListener(onRBCheckedChangeListener);
    }


    @Override
    protected void initData() {
        rbDocket.setChecked(true);
    }

    class MyOnRBCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {


        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (!isChecked) {
                return;
            }
            if (DocketUtil.getInstance().getUpgradeLevel() == AllConstants.UPGRADE_FIRST) {
                ToastUtil.showToast(R.string.upgrade_first);
                LogUtil.getInstance().insertOrReplaceData(getString(R.string.upgrade_log, AppUtil.getInstance().getVersion()));
                return;
            }
            if (buttonView != rbDocket) {
                EventBus.getDefault().post(new Event(EventCode.EVENT_HIDE_REVERT_SIGN));
            }
            switch (buttonView.getId()) {
                case R.id.rb_docket:
                    if (DocketUtil.getInstance().isLogDetail()) {
                        fragmentIndex = 3;
                    } else {
                        fragmentIndex = 0;
                    }
                    break;
                case R.id.rb_driver:
                    fragmentIndex = 1;
                    LogUtil.getInstance().insertOrReplaceData("查看送貨單" + DocketUtil.getInstance().getDocketNo() + "司機");
                    break;
                case R.id.rb_code:
                    fragmentIndex = 2;
                    LogUtil.getInstance().insertOrReplaceData("查看送貨單" + DocketUtil.getInstance().getDocketNo() + "二維碼");
                    break;
            }
            showFragment(mFragment, getFragment());
        }
    }

    private Fragment getFragment() {
        return fragmentList.get(fragmentIndex);
    }

    private void showFragment(Fragment fromFragment, Fragment toFragment) {
        if (fromFragment != toFragment) {
            mFragment = toFragment;
            FragmentManager fm = getChildFragmentManager();
            FragmentTransaction transaction = fm.beginTransaction();
            if (fromFragment != null) {
                transaction.hide(fromFragment);
            }
            if (toFragment.isAdded()) {
                transaction.show(toFragment);
            } else {
                transaction.add(R.id.fl_content, toFragment);
            }
            transaction.commit();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Fragment fragment = getFragment();
        fragment.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        Fragment fragment = getFragment();
        fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Event event) {
        int what = event.getWhat();
        switch (what) {
            case EventCode.EVENT_ARRIVE_PLANT:
                new Handler().postDelayed(() -> rbDocket.setChecked(true), 500);
                break;
            case EventCode.EVENT_ARRIVE_SITE:
            case EventCode.EVENT_OK:
            case EventCode.EVENT_ADDITIONAL:
            case EventCode.EVENT_ON_HAND:
                rbDocket.setChecked(true);
                break;
            case EventCode.EVENT_DONE_QC_CHECK:
            case EventCode.EVENT_DONE_COMPLETE:
            case EventCode.EVENT_DONE_REVERT_SIGN:
                rbDriver.setChecked(true);
                break;
            case EventCode.EVENT_LOG_DETAIL:
                if (rbDocket.isChecked()) {
                    fragmentIndex = 3;
                    showFragment(mFragment, getFragment());
                } else {
                    rbDocket.setChecked(true);
                }
                break;
            case EventCode.EVENT_RETURN_DOCKET:
                if (rbDocket.isChecked()) {
                    fragmentIndex = 0;
                    showFragment(mFragment, getFragment());
                } else {
                    rbDocket.setChecked(true);
                }
                break;
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            if (fragmentIndex == 0) {
                EventBus.getDefault().post(new Event(EventCode.EVENT_HIDE_SIGN));
            }
        } else {
            if (!DocketUtil.getInstance().isLogDetail()) {
                if (rbDocket.isChecked()) {
                    if (fragmentIndex == 0) {
                        return;
                    }
                    fragmentIndex = 0;
                    showFragment(mFragment, getFragment());
                } else {
                    rbDocket.setChecked(true);
                }
            }
        }
    }
}
